﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Objects;
using System.Data.Entity;
using System.Linq.Expressions;
using System.Data.Entity.Infrastructure;

namespace ZD_Repositories
{
    public class RepositoryBase<T> where T : class
    {
        private DbContext context;
        private DbSet<T> dbSet;
        public RepositoryBase(DbContext _context)
        {
            this.context = _context;
            this.dbSet = this.context.Set<T>();
        }


        public T FindOne(object id)
        {
            return dbSet.Find(id);
        }

        public void GetList(Expression<Func<T, bool>> where)
        {
            dbSet.Where(where);
        }

        public void Add(T model)
        {
            dbSet.Add(model);
        }

        public void Delete(T model)
        {
            var a = context.Entry<T>(model);
            //((IObjectContextAdapter)context).ObjectContext.ObjectStateManager.AttachTo(model.GetType().Name, model);
            a.State = EntityState.Deleted;
        }
    }
}
